package com.lanyou.esb.cook.proxy.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.RowMapper;

import com.lanyou.esb.cook.proxy.entity.IfReqQuantity;

/**
 * 接口请求量控制持久层
 * 
 * @author Davey.wu
 */
public class IfReqQuantityDao extends BaseDao<IfReqQuantity> {
	RowMapper<IfReqQuantity> mapper = new RowMapper<IfReqQuantity>() {
		public IfReqQuantity mapRow(ResultSet rs, int rowNum)
				throws SQLException {
			IfReqQuantity rcd = new IfReqQuantity();
			rcd.setId(rs.getString("IF_REQUEST_QTY_ID"));
			rcd.setIfId(rs.getString("IF_ID"));
			rcd.setReqThreshold(rs.getInt("REQUEST_THRESHOLD"));
			rcd.setReqMax(rs.getInt("MAX_REQUEST_QTY"));
			rcd.setReqUnit(rs.getString("REQUEST_UNIT"));
			return rcd;
		}
	};

	@Override
	public List<IfReqQuantity> findAll() {
		String sql = "select IF_REQUEST_QTY_ID,IF_ID,REQUEST_THRESHOLD,MAX_REQUEST_QTY,REQUEST_UNIT from  t_esb_bu_if_request_qty";
		List<IfReqQuantity> records = this.getJdbcTemplate().query(sql, mapper);
		return records;
	}

	@Override
	public IfReqQuantity find(IfReqQuantity quantity) {
		// 根据接口ID，请求单位获取接口请求量控制信息
		if (quantity == null || StringUtils.isBlank(quantity.getIfId())
				|| StringUtils.isBlank(quantity.getReqUnit()))
			;
		String sql = "select IF_REQUEST_QTY_ID,IF_ID,REQUEST_THRESHOLD,MAX_REQUEST_QTY,REQUEST_UNIT from  t_esb_bu_if_request_qty where IF_ID=? and REQUEST_UNIT=?";
		IfReqQuantity record = this.getJdbcTemplate().queryForObject(sql,
				mapper, quantity.getIfId(), quantity.getReqUnit());
		return record;
	}
}
